package com.awesome.javademo.lang;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.awesome.javademo.R;
import com.awesome.javademo.lang.bean.People;
import com.awesome.common.util.log.ShowLogUtil;

public class HashCodeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hash_code);

        // 基本类型判断
        ShowLogUtil.info("基本数据类型不能比较比较equals");
        ShowLogUtil.info("基本数据类型比较==(123==123)=" + (123 == 123));
        // String类型判断
        ShowLogUtil.info("字符串比较equals(\"123\".equals(\"123\"))=" + ("123".equals("123")));
        ShowLogUtil.info("字符串比较==(\"123\"==\"123\")=" + ("123" == "123"));
        // new String()类型判断
        ShowLogUtil.info("new String类型比较equals(new String(\"123\").equals(new String(\"123\")))=" + (new String("123").equals(new String("123"))));
        ShowLogUtil.info("new String类型比较==(new String(\"123\")==new String(\"123\"))=" + (new String("123") == new String("123")));

        //

        /**
         * equals、hashCode相等，=="不一定"相等
         * ==相等,equals、hashCode"一定"相等
         * 默认情况下,没有复写equals、hashCode时，
         * hashCode相等equals”不一定“相等，equals相等hashCode“一般”相等
         * 复写equals、hashCode时
         * 根据复写内容有关
         */

        // 新创建两个对象进行比较
        People a1 = new People(1, "Alice", 24);
        People a2 = new People(1, "Alice", 24);
        ShowLogUtil.info("a1的toString()=" + a1.toString());
        ShowLogUtil.info("a2的toString()=" + a2.toString());
        ShowLogUtil.info("对象类型比较equals(a1.equals(a2))=" + a1.equals(a2));
        // equal、hashCode相等，=="不一定"相等
        ShowLogUtil.info("对象类型比较==(a1==a2)=" + (a1 == a2));
        ShowLogUtil.info("a1的HashCode=" + a1.hashCode());
        ShowLogUtil.info("a2的HashCode=" + a2.hashCode());

        // 两个相等的对象进行比较
        People b1 = new People(1, "Alice", 23);
        People b2 = b1;
        ShowLogUtil.info("b1的toString()=" + b1.toString());
        ShowLogUtil.info("b2的toString()=" + b2.toString());
        ShowLogUtil.info("对象类型比较equals(b1.equals(b2))=" + b1.equals(b2));
        // ==相等,equal、hashCode"一定"相等
        ShowLogUtil.info("对象类型比较==(b1==b2)=" + (b1 == b2));
        ShowLogUtil.info("b1的HashCode=" + b1.hashCode());
        ShowLogUtil.info("b2的HashCode=" + b2.hashCode());
    }
}